/* Android-Michi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * Android-Michi is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Android-Michi; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */
package controller;

import com.chaski.michi.R;

import model.ComputerTicTacToe;
import view.DisplayManager;

public class OnePlayerMichi implements ButtonInterface{
	DisplayManager displayManager;
	ComputerTicTacToe ticTacToe = new ComputerTicTacToe();
	char player = 'X';
	
	public  OnePlayerMichi(DisplayManager _displayManager) {
		displayManager = _displayManager;
	}

	@Override
	public void btn00Event() {
		if (!ticTacToe.yourMove(0))
		{
			displayManager.displayMessage(R.string.funny_error_string);
			return;
		}
		displayManager.display00(player);
		
		int computer = ticTacToe.myMove();
		if (computer != -1) 
				displayO(computer);
		
		checkWinner();
			 		
	}

	@Override
	public void btn01Event() {
		if (ticTacToe.yourMove(1))
		{
			displayManager.display01(player);
			int computer = ticTacToe.myMove();
			if (computer != -1) {
				displayO(computer);
			}
			checkWinner();
		}
		else
	 		displayManager.displayMessage(R.string.funny_error_string);
	}

	@Override
	public void btn02Event() {
		if (ticTacToe.yourMove(2))
		{
			displayManager.display02(player);
			int computer = ticTacToe.myMove();
			if (computer != -1) {
				displayO(computer);
			}
			checkWinner();
		}
		else
	 		displayManager.displayMessage(R.string.funny_error_string);
		
	}

	@Override
	public void btn10Event() {
		if (ticTacToe.yourMove(3))
		{
			displayManager.display10(player);
			int computer = ticTacToe.myMove();
			if (computer != -1) {
				displayO(computer);
			}
			checkWinner();
		}
		else
	 		displayManager.displayMessage(R.string.funny_error_string);
	}

	@Override
	public void btn11Event() {
		if (ticTacToe.yourMove(4))
		{
			displayManager.display11(player);
			int computer = ticTacToe.myMove();
			if (computer != -1) {
				displayO(computer);
			}
			checkWinner();
		}
		else
	 		displayManager.displayMessage(R.string.funny_error_string);
		
	}

	@Override
	public void btn12Event() {
		if (ticTacToe.yourMove(5))
		{
			displayManager.display12(player);
			int computer = ticTacToe.myMove();
			if (computer != -1) {
				displayO(computer);
			}
			checkWinner();
		}
		else
	 		displayManager.displayMessage(R.string.funny_error_string);
	}

	@Override
	public void btn20Event() {
		if (ticTacToe.yourMove(6))
		{
			displayManager.display20(player);
			int computer = ticTacToe.myMove();
			if (computer != -1) {
				displayO(computer);
			}
			checkWinner();
		}
		else
	 		displayManager.displayMessage(R.string.funny_error_string);
		
	}

	@Override
	public void btn21Event() {
		if (ticTacToe.yourMove(7))
		{
			displayManager.display21(player);
			int computer = ticTacToe.myMove();
			if (computer != -1) {
				displayO(computer);
			}
			checkWinner();
		}
		else
	 		displayManager.displayMessage(R.string.funny_error_string);
		
	}

	@Override
	public void btn22Event() {
		if (ticTacToe.yourMove(8))
		{
			displayManager.display22(player);
			int computer = ticTacToe.myMove();
			if (computer != -1) {
				displayO(computer);
			}
			checkWinner();
		}
		else
	 		displayManager.displayMessage(R.string.funny_error_string);
		
	}

	private void displayO(int value)
	{
		char player = '0';
		
		switch (value) {
		case 0:
			displayManager.display00(player);
			break;
		case 1:
			displayManager.display01(player);
			break;
		case 2:
			displayManager.display02(player);
			break;
		case 3:
			displayManager.display10(player);
			break;
		case 4:
			displayManager.display11(player);
			break;
		case 5:
			displayManager.display12(player);
			break;
		case 6:
			displayManager.display20(player);
			break;
		case 7:
			displayManager.display21(player);
			break;
		case 8:
			displayManager.display22(player);
			break;
		}
	}

	public boolean checkWinner() {
		switch(ticTacToe.status()) {
		case ComputerTicTacToe.WIN:
			displayManager.displayWinner(R.string.computer_string);
			return true;
		case ComputerTicTacToe.LOSE:
			displayManager.displayWinner(R.string.human_string);
			return true;
		case ComputerTicTacToe.STALEMATE:
			displayManager.displayWinner(R.string.nobody_string);
			return true;
		default:
			return false;
		}
	}
}
